# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : t6.py
# Date      : 2020/4/1 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import os
import sys
import shutil

from moviepy.editor import *


mu_org_path = r"/storage/emulated/0/Android/data/com.xiaomi.smarthome/files/310733424"

root_path = r"E:\xiaomi\310733424"
dir_list = os.listdir(root_path)
output = []
for dl in dir_list:
    c_path = os.path.join(root_path,dl)
    c_list = os.listdir(os.path.join(root_path,dl))
    i = 0
    
    for cl in c_list:
        if ".mp4" in cl:
            i+=1
        if "_plain.m3u8" in cl:
            mu = cl


    if not i:
        if os.path.isfile(c_path):  # 判断该文件是否为文件或者文件夹
            os.remove(c_path)  # 若为文件，则直接删除
            print(str(c_path) + " removed!")
        elif os.path.isdir(c_path):
            shutil.rmtree(c_path, True)  # 若为文件夹，则删除该文件夹及文件夹内所有文件
            print("dir " + str(c_path) + " removed!")
    
    else:
        mu_path = os.path.join(c_path,mu)
        if os.path.exists(mu_path):
            # with open(mu_path,) as fs:
            #     data = fs.read()
            #     data_line = fs.readlines()
            #     print data_line
            
            _org_path = os.path.join(mu_org_path,dl).replace("\\","/")
            
            fs = open(mu_path)
            data_line = fs.readlines()
            index = []
            for _line in data_line:
                if _org_path in _line:
                    idx = _line.replace(_org_path,c_path).rsplit("\n",1)[0]
                    index.append(idx)
                    output.append(idx)
            fs.close()

            # print index
            
            # print data
            # redata = data.replace(_org_path,c_path)
            #
            # with open(mu_path.replace("_plain.","_output."),"w") as fw:
            #     fw.write(redata)
            
            # ---
            
            # print "================================"
            # print dl
            # print "star for >>> VideoFileClip"
            
            # output_path = os.path.join(r"E:\xiaomi\output",dl+".mp4")
            # for root, dirs, files in os.walk(c_path):
            #     for _file in files:
            #         # 如果后缀名为 .mp4
            #         if os.path.splitext(_file)[1] == '.mp4':
            # video_list = []
            # for root, dirs, files in os.walk(c_path):
            #     # 按文件名排序
            #     # files.sort()
            #     # 遍历所有文件
            # pat = None
            # i = 0
            # _ddd = []
            # for _file in index:
            #     # 如果后缀名为 .mp4
            #     if os.path.splitext(_file)[1] == '.mp4':
            #         # 拼接成完整路径
            #         # filePath = os.path.join(root, _file)
            #         filePath = _file
            #
            #         # 载入视频
            #         video = VideoFileClip(filePath)
            #         # print video.fps
            #         # 添加到数组
            #         # video_list.append(video)
            #         if pat :
            #             final_clip = concatenate_videoclips([pat,video])
            #             output_path = os.path.join(r"E:\xiaomi\output", dl + "%s.mp4"%i)
            #             _ddd.append(output_path)
            #             final_clip.write_videofile(output_path, remove_temp=False)
            #             pat.close()
            #
            #             pat = VideoFileClip(output_path)
            #             video.close()
            #
            #         else:
            #             pat = video
            #         i+=1
            # pat.close()
            # [os.remove( _)for _ in _ddd[:-1]]
            # os.remove(video_list[:-1])
            # 拼接视频
            
            # print dl + "  >>>  {}" .format(  video_list)
            # print len(video_list)
            # print "star for >>> concatenate_videoclips"
            # final_clip = concatenate_videoclips(video_list)

            # 生成目标视频文件
            # final_clip.to_videofile(output_path, remove_temp=False)
            # print "star for >>> write_videofile"
            # final_clip.write_videofile(output_path, remove_temp=False)
            # print "end for >>> write_videofile"
            # [_.close() for _ in video_list]
            
        else:
            print mu_path
        # print data
for i,o in enumerate(output,1):
    try:
        shutil.copy(o,r"E:\xiaomi\output\%s.mp4"%i)
    except:print o


if __name__ == '__main__':
    pass